package cn.haohaowo.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.haohaowo.common.SessionContainer;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import cn.haohaowo.common.Constants;
import cn.haohaowo.entity.Account;
import cn.haohaowo.service.AccountBizService;
import cn.haohaowo.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Getter
@Setter
@Controller
@RequestMapping("/account")
public class AccountController {

	private static final long serialVersionUID = 5583859910624730852L;

	private String username;
	private String password;
	private String rememberMe;
	private String returnPath;
	private String orginalPassword;
	private String newPassword;
	private String validationCode;
	private String notice;
	private String loginMsg;
	
	private Account account;

	private SessionContainer sessionContainer;
	
	@Autowired
	private AccountBizService accountBizService;

    @RequestMapping("/register.do")
	public String register(HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession();
		if("post".equalsIgnoreCase(request.getMethod())){
			accountBizService.saveOrUpdateAccount(account);
			sessionContainer = (SessionContainer) session.getAttribute(Constants.SESSION_CONTAINER_KEY);
			sessionContainer.setAccount(account);
			session.setAttribute(Constants.SESSION_CONTAINER_KEY, sessionContainer);
			return Constants.REGISTER_SUCCESS;
		}else{
			account = new Account();
		}
		return Constants.REGISTER;
	}

    @RequestMapping("/registerSuccess.do")
	public String registerSuccess(){
		return Constants.REGISTER_SUCCESS_PAGE;
	}

	@RequestMapping("/password.do")
	public String password(HttpServletRequest request, HttpServletResponse response){
		if("post".equalsIgnoreCase(request.getMethod())){
			if(account.getPassword().equals(orginalPassword)){
				account.setPassword(newPassword);
				accountBizService.saveOrUpdateAccount(account);
				notice = "密码修改成功";
			}else{
				notice = "密码不对";
			}
			request.setAttribute("notice", notice);
		}
		return Constants.PASSWORD;
	}

	@RequestMapping("/update.do")
	public String update(HttpServletRequest request, HttpServletResponse response){
		accountBizService.saveOrUpdateAccount(account);
		notice = "保存成功";
		request.setAttribute("notice", notice);
		
		return Constants.MY;
	}


	@RequestMapping("/my.do")
	public String my(){
		return Constants.MY;
	}

	@RequestMapping("/login.do")
	public String login(HttpServletRequest request, HttpServletResponse response){
		if("get".equalsIgnoreCase(request.getMethod())){
			if(!loggedIn()){
				return Constants.LOGIN;
			}
		} else if("post".equalsIgnoreCase(request.getMethod())) {
			Account account = accountBizService.login(username, password);
			HttpSession session = request.getSession();
			if(null != account) {
				if("true".equals(rememberMe)){
					rememberMe(request, response, username, password);
				}

				sessionContainer = (SessionContainer) session.getAttribute(Constants.SESSION_CONTAINER_KEY);
				sessionContainer.setAccount(account);
				session.setAttribute(Constants.SESSION_CONTAINER_KEY, sessionContainer);
				returnPath = (String) session.getAttribute(Constants.RETURN_PATH);
				if(StringUtils.isNotEmpty(returnPath)){
					session.removeAttribute(Constants.RETURN_PATH);
					return returnPath;
				}
			} else {
				loginMsg = "用户密码输入有误";
				return Constants.LOGIN;
			}
		}
		
		return Constants.HOME;
	}

    private boolean loggedIn() {
        return false;
    }

    @RequestMapping("/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response, String username){
		request.getSession().setAttribute(username, null);
		forgotMe(request, response);
		return Constants.HOME;
	}

	private void rememberMe(HttpServletRequest request,
							HttpServletResponse response,
							String username,
							String password){
		Cookie cookie = new Cookie(Constants.REMEMBER_ME_KEY,
				username + Constants.REMEMBER_ME_SEPERATOR + password);
		cookie.setPath("/");
		cookie.setMaxAge(60 * 60 * 24 * 7 * 2);
		response.addCookie(cookie);
	}
	
	private void forgotMe(HttpServletRequest request, HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		for(Cookie cookie:cookies){
			if(cookie.getName().equals(Constants.REMEMBER_ME_KEY)){
				cookie.setPath("/");
				cookie.setMaxAge(0);
				cookie.setValue(null);
				response.addCookie(cookie);
				break;
			}
		}
	}

	@ResponseBody
	@RequestMapping("/emailExist.do")
	public String emailExist(HttpServletRequest request, HttpServletResponse response, int accountId, String email) {
		Boolean isExist = accountBizService.emailExist(accountId, email);
		return isExist.toString();
	}

	@ResponseBody
	@RequestMapping("/usernameExist.do")
	public String usernameExist(HttpServletRequest request, HttpServletResponse response, String userName) {
		Boolean isExist = accountBizService.usernameExist(userName);
		return isExist.toString();
	}
	
}
